<?php
class SearchAction extends PublicAction {

	/**
	 * 跳转进入搜索页面
	 * @author 沈威
	 * @tel 15005164276
	 * @email 403382132@qq.com
	 * Enter description here ...
	 */
	public function index()
	{
		$lap['status']=1;
		$lap['is_dele']=0;
		if($_POST['category'])
		{
			$cateid=$_POST['category'];
		}
		else 
		{
			$cateid=0;
		}
		
		if($_GET['key'])
		{
			$keyword=$_GET['key'];
		}
		
		if(!$_GET['p'])
		{
			$p=1;
		}
		else 
		{
			$p=$_GET['p'];
		}
		
		if($_POST['search_keyword'])
		{
			$keyword=$_POST['search_keyword'];
		}
		
		$lap['title']=array('like','%'.$keyword.'%');
		
		$pgnum=15;
		
			if($_POST['bodysearch']==1)
			{
				$bything=$_POST['bything'];
				if($bything==1)
				{
					$keyword=$_POST['keyword1'];
				}
				else 
				{
					$keyword=$_POST['keyword2'];
				}
				
				if(isset($_SESSION['zkeyword']))
				{
					array_push($_SESSION['zkeyword'],$keyword);
					$_SESSION['zkeyword']=array_unique($_SESSION['zkeyword']);
				}
				else 
				{	
					$zkeyword=array();
					array_push($zkeyword,$keyword);
					$_SESSION['zkeyword']=$zkeyword;
				}
				$this->assign('zkeywords',$_SESSION['zkeyword']);
				
				
				$stime=$_POST['stime'];
				if($stime==1)
				{
					$btime=strtotime($_POST['btime']);
					$etime=strtotime($_POST['etime']);
					$btime_p=$_POST['btime'];
					$etime_p=$_POST['etime'];
					$ptime=strtotime($_POST['etime'])+86400;
					$ztime=strtotime($_POST['btime'])+86400;
					if($btime!=''&&$etime!='')
					{
						if($btime==$etime)
						{
							$lap['uploadtime']=array(array('gt',$btime),array('lt',$ptime));
						}
						elseif($btime>$etime)
						{
							$ntime=$btime;
							$btime=$etime;
							$etime=$ntime;
							$lap['uploadtime']=array(array('gt',$btime),array('lt',$ztime)) ;
						}
						else 
						{
							$lap['uploadtime']=array(array('gt',$btime),array('lt',$ptime)) ;
						}
					}
					else 
					{
						if(!($btime==''&&$etime==''))
						{
							if($btime=='')
							{
								$lap['uploadtime']=array('lt',$ptime);
							}
							else 
							{
								$lap['uploadtime']=array('gt',$btime);
							}
						}
					}
				}
				else 
				{
					$day=$_POST['ftime'];
					switch ($day)
					{
						case 2:$lap['uploadtime']=array(array('gt',time()-86400),array('lt',time())) ;break;
						case 3:$lap['uploadtime']=array(array('gt',time()-172800),array('lt',time())) ;break;
						case 4:$lap['uploadtime']=array(array('gt',time()-604800),array('lt',time())) ;break;
						case 5:$lap['uploadtime']=array(array('gt',time()-1296000),array('lt',time())) ;break;
					}
				}
				
				$bything=$_POST['bything'];
				if($bything==1)
				{
					$keyword=$_POST['keyword1'];
				}
				else 
				{
					$keyword=$_POST['keyword2'];
				}
				
				//title属性未做完
				if($_POST['submit']==1)
				{
					if($bything==1)
					{
						$lap['title']=array('like','%'.$keyword.'%');
					}
					else 
					{
						$lengh=strlen($keyword);
						$num=0;
						if($keyword!="")
						{
							$lap['title']=array();
							while($num<$lengh)
							{
								$zi=mb_substr($keyword,$num,3);
								array_push($lap['title'],array('like','%'.$zi.'%'));
								$num+=3;
							}
						}
					}
				}
				else 
				{
					if($bything==1)
					{
						$lap['title']=array(array('like','%'.$keyword.'%'),array('like','%'.$_SESSION['lastkey'].'%'));
					}
					else 
					{
						$keyword.=$_SESSION['lastkey'];
						$lengh=strlen($keyword);
						$num=0;
						if($keyword!="")
						{
							$lap['title']=array();
							while($num<$lengh)
							{
								$zi=mb_substr($keyword,$num,3);
								array_push($lap['title'],array('like','%'.$zi.'%'));
								$num+=3;
							}
						}
					}
					
				}//_POST['submit']  End

				//完全匹配
				if($_POST['all_key'])
				{
					$all_key=$_POST['all_key'];
				}
				//全库搜索
				if($_POST['all_sql'])
				{
					$all_sql=$_POST['all_sql'];
				}
				
				if($all_key==1)
				{
					$lap['title']=$keyword;
				}
				if($all_sql==2)
				{
					unset($lap['is_dele']);
					unset($lap['status']);
				}
				
				$order=$_POST['orderby'];
				switch ($order)
				{
					case 1:$orderby="uploadtime desc";break;
					case 2:$orderby="uploadtime asc";break;
					case 3:$orderby="is_hot desc,uploadtime desc";break;//相关度不知道什么意思待处理
				}
				$pgnum = $_POST['count'];
				$_SESSION['lastkey']=$keyword;
				
			}//post_end
			
			
	if($_GET)
		{
			if(count($_GET)==1)
			{
				unset($_GET);
			}
			elseif($_GET['PHPSESSID']!="")
			{
				unset($_GET);
			}
			else 
			{
				$lap=$_GET;
				unset($lap['_URL_']);
				unset($lap['PHPSESSID']);
				$lap['is_dele']=0;
				$lap['status']=1;
				if($_GET['pgnum'])
				{
					$pgnum=$_GET['pgnum'];
				}
				$lap['title']=array('like','%'.$keyword.'%');
				if($_GET['key'])
				{
					$keyword=$_GET['key'];
					$lap['title']=array('like','%'.$keyword.'%');
					$where['code']  = $keyword;
					$where['title']  = $lap['title'];
					$where['_logic'] = 'or';
					$lap['_complex'] = $where;
					
					unset($lap['key']);
					unset($lap['title']);
				}
				if($_GET['stime'])
				{
					$stime=$_GET['stime'];
		
					if($stime==1)
					{
						$btime=strtotime($_GET['btime']);
						$etime=strtotime($_GET['etime']);
						$btime_p=$_GET['btime'];
						$etime_p=$_GET['etime'];
						$ptime=strtotime($_GET['etime'])+86400;
						$ztime=strtotime($_GET['btime'])+86400;
						if($btime!=''&&$etime!='')
						{
							if($btime==$etime)
							{
								$lap['uploadtime']=array(array('gt',$btime),array('lt',$ptime));
							}
							elseif($btime>$etime)
							{
								$ntime=$btime;
								$btime=$etime;
								$etime=$ntime;
								$lap['uploadtime']=array(array('gt',$btime),array('lt',$ztime)) ;
							}
							else 
							{
								$lap['uploadtime']=array(array('gt',$btime),array('lt',$ptime)) ;
							}
						}
						else 
						{
							if(!($btime==''&&$etime==''))
							{
								if($btime=='')
								{
									$lap['uploadtime']=array('lt',$ptime);
								}
								else 
								{
									$lap['uploadtime']=array('gt',$btime);
								}
							}
						}
					}
					else 
					{
						$day=$_GET['ftime'];
						switch ($day)
						{
							case 2:$lap['uploadtime']=array(array('gt',time()-86400),array('lt',time())) ;break;
							case 3:$lap['uploadtime']=array(array('gt',time()-172800),array('lt',time())) ;break;
							case 4:$lap['uploadtime']=array(array('gt',time()-604800),array('lt',time())) ;break;
							case 5:$lap['uploadtime']=array(array('gt',time()-1296000),array('lt',time())) ;break;
						}
					}
				}
				//完全匹配
				if($_GET['all_key'])
				{
					$all_key=$_GET['all_key'];
				}
				//全库搜索
				if($_GET['all_sql'])
				{
					$all_sql=$_GET['all_sql'];
				}
				
				if($all_key==1)
				{
					if($keyword!="")
					{
						$lap['title']=$keyword;
					}
				}
				if($all_sql==2)
				{
					unset($lap['is_dele']);
					unset($lap['status']);
				}
				
				$order=$_GET['orderby'];
				switch ($order)
				{
					case 1:$orderby="uploadtime desc";break;
					case 2:$orderby="uploadtime asc";break;
					case 3:$orderby="is_hot desc,uploadtime desc";break;
				}
				unset($lap['orderby']);
				unset($lap['pgnum']);
				unset($lap['all_key']);
				unset($lap['all_sql']);
				unset($lap['p']);
				unset($lap['category']);
			}
		}
			
			
			
			if(!$pgnum)
			{
				$pgnum=15;
			}
			if(!$orderby)
			{
				$orderby='uploadtime desc';
			}
			
				$pictureView = D('PictureView');
				$videoView = D('VideoView');
				$gameView = D('GameView');
				$flashView = D('FlashView');
				$bookView = D('BookView');
				$exhibitsView = D('ExhibitsView');
				$knowledgeView = D('KnowledgeView');
				$articleView= D('ArticleView');
				$imageView = D('ImageView');
				
			
				if(!($lap['_complex']))
				{
					$where['code']  = $keyword;
					$where['title']  = $lap['title'];
					$where['_logic'] = 'or';
					$lap['_complex'] = $where;
				}
				unset($lap['title']);
				unset($lap['ftime']);
				unset($lap['stime']);
				unset($lap['btime']);
				unset($lap['etime']);
				//print_r($lap);
				
				switch ($p)
				{
					case 1:
						$picturelist=$pictureView->where($lap)->order('uploadtime desc')->limit("0,5")->select();
						$videolist=$videoView->where($lap)->order('uploadtime desc')->limit("0,5")->select();
						$flashlist=$flashView->where($lap)->order('uploadtime desc')->limit("0,5")->select();
						foreach($picturelist as $k => $v)
						{
							$url = explode(',', $v['url']);
							$picturelist[$k]['url']=$url[0];
							//$list[$k]['desc'] = $picturemodel->where($hap)->getField('desc');
						}
						$this->assign('picturelist',$picturelist);
						$this->assign('videolist',$videolist);
						$this->assign('flashlist',$flashlist);
						break;
					case 2:
						$gamelist=$gameView->where($lap)->order('uploadtime desc')->limit("0,5")->select();
						$articlelist=$articleView->where($lap)->order('uploadtime desc')->limit("0,5")->select();
						//print_r($articleView->getLastSql());
						$imagelist=$imageView->where($lap)->order('uploadtime desc')->limit("0,5")->select();
						foreach($imagelist as $k => $v)
						{
							$url = explode(',', $v['url']);
							$imagelist[$k]['url']=$url[0];
							//$list[$k]['desc'] = $picturemodel->where($hap)->getField('desc');
						}
						$this->assign('gamelist',$gamelist);
						$this->assign('articlelist',$articlelist);
						$this->assign('imagelist',$imagelist);
						break;
					case 3:
						$booklist=$bookView->where($lap)->order('uploadtime desc')->limit("0,5")->select();
						$exhibitslist=$exhibitsView->where($lap)->order('uploadtime desc')->limit("0,5")->select();
						$knowledgelist=$knowledgeView->where($lap)->order('uploadtime desc')->limit("0,5")->select();
						foreach($exhibitslist as $k => $v)
						{
							$url = explode(',', $v['url']);
							$exhibitslist[$k]['url']=$url[0];
							//$list[$k]['desc'] = $picturemodel->where($hap)->getField('desc');
						}
						$this->assign('booklist',$booklist);
						$this->assign('exhibitslist',$exhibitslist);
						$this->assign('knowledgelist',$knowledgelist);
						break;
				}
				$this->assign('p',$p);
				//echo $keyword;
				if(trim($keyword)!="")
				{
					$this->assign('ykey',$keyword);
				}
				$this->assign('cateid',$cateid);
				$this->assign('stime',$stime);
				$this->assign('ftime',$day);
				$this->assign('all_key',$all_key);
				$this->assign('all_sql',$all_sql);
				$this->assign('btime',$btime_p);
				$this->assign('etime',$etime_p);
				$tiaojian=$lap;
				$tiaojian['keyword1']=$keyword;
				$tiaojian['count']=$pgnum;
				$tiaojian['ftime']=$day;
				$tiaojian['stime']=$stime;
				$tiaojian['btime']=$btime_p;
				$tiaojian['etime']=$etime_p;
				$tiaojian['all_key']=$all_key;
				$tiaojian['all_sql']=$all_sql;
				$tiaojian['orderby']=$order;
				//print_r($tiaojian);
				$this->assign('choose',$tiaojian);
				
		//获取最近检索词
		$zkeywords=$_SESSION['zkeyword'];
		$this->assign('zkeywords',$zkeywords);
				
				$this->display();
				
	}
    
	/**
	 * 跳转到更多页面
	 * Enter description here ...
	 */
	public function more()
	{
		if($_POST['search_keyword'])
		{
			$keyword=$_POST['search_keyword'];
			$wap['title']=array('like','%'.$keyword.'%');
			$where['code']  = $keyword;
			$where['title']  = $wap['title'];
			$where['_logic'] = 'or';
			$wap['_complex'] = $where;
			unset($wap['title']);
		}
		if($_GET['category'])
		{
			$category=$_GET['category'];
		}
		else 
		{
			$category=$_POST['category'];
		} 		
		
		$category_model=M('Category');
		$rap['cateid']=$category;
		$category_one=$category_model->where($rap)->Field('cateid,catename,engname')->find();
		//print_r($category_model->getLastSql());
		$this->assign('category',$category_one);
		
		$wap['status']=1;
		$wap['is_dele']=0;
		
		import("ORG.Util.Page");
		if($_POST['bodysearch']==1)
			{
				$bything=$_POST['bything'];
				if($bything==1)
				{
					$keyword=$_POST['keyword1'];
				}
				else 
				{
					$keyword=$_POST['keyword2'];
				}
				
				if(isset($_SESSION['zkeyword']))
				{
					array_push($_SESSION['zkeyword'],$keyword);
					$_SESSION['zkeyword']=array_unique($_SESSION['zkeyword']);
				}
				else 
				{
					
					$zkeyword=array();
					array_push($zkeyword,$keyword);
					$_SESSION['zkeyword']=$zkeyword;
				}
				$this->assign('zkeywords',$_SESSION['zkeyword']);
				//$this->assign('choose',$_POST);
				
				$stime=$_POST['stime'];
				if($stime==1)
				{
					$btime_p=$_POST['btime'];
					$etime_p=$_POST['etime'];
					$btime=strtotime($_POST['btime']);
					$etime=strtotime($_POST['etime']);
					$ptime=strtotime($_POST['etime'])+86400;
					$ztime=strtotime($_POST['btime'])+86400;
					if($btime!=''&&$etime!='')
					{
						if($btime==$etime)
						{
							$wap['uploadtime']=array(array('gt',$btime),array('lt',$ptime));
						}
						elseif($btime>$etime)
						{
							$ntime=$btime;
							$btime=$etime;
							$etime=$ntime;
							$wap['uploadtime']=array(array('gt',$btime),array('lt',$ztime)) ;
						}
						else 
						{
							$wap['uploadtime']=array(array('gt',$btime),array('lt',$ptime)) ;
						}
					}
					else 
					{
						if(!($btime==''&&$etime==''))
						{
							if($btime=='')
							{
								$wap['uploadtime']=array('lt',$ptime);
							}
							else 
							{
								$wap['uploadtime']=array('gt',$btime);
							}
						}
					}
				}
				else 
				{
					$day=$_POST['ftime'];
					switch ($day)
					{
						case 2:$wap['uploadtime']=array(array('gt',time()-86400),array('lt',time())) ;break;
						case 3:$wap['uploadtime']=array(array('gt',time()-172800),array('lt',time())) ;break;
						case 4:$wap['uploadtime']=array(array('gt',time()-604800),array('lt',time())) ;break;
						case 5:$wap['uploadtime']=array(array('gt',time()-1296000),array('lt',time())) ;break;
					}
				}
				
				$bything=$_POST['bything'];
				if($bything==1)
				{
					$keyword=$_POST['keyword1'];
				}
				else 
				{
					$keyword=$_POST['keyword2'];
				}
				$all_key=$_POST['all_key'];
				$all_sql=$_POST['all_sql'];
				
				
				//title属性未做完
				if($_POST['submit']==1)
				{
					if($bything==1)
					{
						$wap['title']=array('like','%'.$keyword.'%');
						$where['code']  = $keyword;
						$where['title']  = $wap['title'];
						$where['_logic'] = 'or';
						$wap['_complex'] = $where;
						unset($wap['title']);
					}
					else 
					{
						$lengh=strlen($keyword);
						$num=0;
						if($keyword!="")
						{
							$wap['title']=array();
							while($num<$lengh)
							{
								$zi=mb_substr($keyword,$num,3);
								array_push($wap['title'],array('like','%'.$zi.'%'));
								$num+=3;
							}
						}
					}
				}
				else 
				{
					if($bything==1)
					{
						$wap['title']=array(array('like','%'.$keyword.'%'),array('like','%'.$_SESSION['lastkey'].'%'));
					}
					else 
					{
						$keyword.=$_SESSION['lastkey'];
						$lengh=strlen($keyword);
						$num=0;
						if($keyword!="")
						{
							$wap['title']=array();
							while($num<$lengh)
							{
								$zi=mb_substr($keyword,$num,3);
								array_push($wap['title'],array('like','%'.$zi.'%'));
								$num+=3;
							}
						}
					}
					
				}				
				if($all_key==1)
				{
					$wap['title']=$keyword;
				}
				if($all_sql==2)
				{
					unset($wap['is_dele']);
					unset($wap['status']);
				}
				$order=$_POST['orderby'];
				switch ($order)
				{
					case 1:$orderby="uploadtime desc";break;
					case 2:$orderby="uploadtime asc";break;
					case 3:$orderby="is_hot desc,uploadtime desc";break;//相关度不知道什么意思待处理
				}
				$pgnum = $_POST['count'];
				$_SESSION['lastkey']=$keyword;
			}
		if(!$pgnum)
		{
			$pgnum=15;
		}
		if(!$orderby)
		{
			$orderby='uploadtime desc';
		}
		
		if($_GET)
		{
			if(count($_GET)==1)
			{
				unset($_GET);
			}
			else 
			{
				$wap=$_GET;
				$wap['status']=1;
				$wap['is_dele']=0;
				unset($wap['_URL_']);
				unset($wap['PHPSESSID']);
				if($_GET['pgnum'])
				{
					$pgnum=$_GET['pgnum'];
				}
				if($_GET['key'])
				{
					$keyword=$_GET['key'];
					$wap['title']=array('like','%'.$keyword.'%');
					$where['code']  = $keyword;
					$where['title']  = $wap['title'];
					$where['_logic'] = 'or';
					$wap['_complex'] = $where;
					unset($wap['key']);
					unset($wap['title']);
				}
				if($_GET['stime'])
				{
					$stime=$_GET['stime'];
		
					if($stime==1)
					{
						$btime_p=$_GET['btime'];
						$etime_p=$_GET['etime'];
						$btime=strtotime($_GET['btime']);
						$etime=strtotime($_GET['etime']);
						$ptime=strtotime($_GET['etime'])+86400;
						$ztime=strtotime($_GET['btime'])+86400;
						if($btime!=''&&$etime!='')
						{
							if($btime==$etime)
							{
								$wap['uploadtime']=array(array('gt',$btime),array('lt',$ptime));
							}
							elseif($btime>$etime)
							{
								$ntime=$btime;
								$btime=$etime;
								$etime=$ntime;
								$wap['uploadtime']=array(array('gt',$btime),array('lt',$ztime)) ;
							}
							else 
							{
								$wap['uploadtime']=array(array('gt',$btime),array('lt',$ptime)) ;
							}
						}
						else 
						{
							if(!($btime==''&&$etime==''))
							{
								if($btime=='')
								{
									$wap['uploadtime']=array('lt',$ptime);
								}
								else 
								{
									$wap['uploadtime']=array('gt',$btime);
								}
							}
						}
					}
					else 
					{
						$day=$_GET['ftime'];
						switch ($day)
						{
							case 2:$wap['uploadtime']=array(array('gt',time()-86400),array('lt',time())) ;break;
							case 3:$wap['uploadtime']=array(array('gt',time()-172800),array('lt',time())) ;break;
							case 4:$wap['uploadtime']=array(array('gt',time()-604800),array('lt',time())) ;break;
							case 5:$wap['uploadtime']=array(array('gt',time()-1296000),array('lt',time())) ;break;
						}
					}
				}
				//完全匹配
				if($_GET['all_key'])
				{
					$all_key=$_GET['all_key'];
				}
				//全库搜索
				if($_GET['all_sql'])
				{
					$all_sql=$_GET['all_sql'];
				}
				if($all_key==1)
				{
					if($keyword!="")
					{
						$wap['title']=$keyword;
					}
				}
				if($all_sql==2)
				{
					unset($wap['is_dele']);
					unset($wap['status']);
				}
				
				$order=$_GET['orderby'];
				switch ($order)
				{
					case 1:$orderby="uploadtime desc";break;
					case 2:$orderby="uploadtime asc";break;
					case 3:$orderby="is_hot desc,uploadtime desc";break;
				}
				unset($wap['orderby']);
				unset($wap['all_key']);
				unset($wap['all_sql']);
				unset($wap['p']);
				unset($wap['pgnum']);
				unset($wap['category']);
			}
		}
		
		unset($wap['key']);
		unset($wap['ftime']);
		unset($wap['stime']);
		unset($wap['btime']);
		unset($wap['etime']);
		$table=$category_one['engname'];
		$table= strtolower($table);
		$model=D('');
		$count = $model->Table('qx_'.$table)->where($wap)->count();
		
		if($_GET['pgnum'])
		{
			$pgnum=$_GET['pgnum'];
		}

		$page = new Page($count,$pgnum);
		
		//分页跳转的时候保证查询条件
		 foreach($wap as $key=>$val) {
			if(is_array($val))
			{
				$page->parameter   .=   "key=".$keyword."&";
			}
			else 
			{
				$page->parameter   .=   "$key=".urlencode($val)."&";
			}
		}
		//echo $page->parameter;
		if($day)
		{
			$page->parameter   .=  "ftime=".$day."&";
		}
		if($stime)
		{
			$page->parameter   .=  "stime=".$stime."&";
		}
	if($btime)
		{
			$page->parameter   .=  "btime=".$btime_p."&";
		}
    if($etime)
		{
			$page->parameter   .=  "etime=".$etime_p."&";
		}
		if($all_key)
		{
			$page->parameter   .=  "all_key=".$all_key."&";
		}
    	if($all_sql)
		{
			$page->parameter   .=  "all_sql=".$all_sql."&";
		}
		$page->parameter   .=  "orderby=".$order."&";
		$page->parameter   .=  "pgnum=".$pgnum."&";
		$page->parameter   .=  "category=".$category;
		
		
		//print_r($wap);
		$list=$model->Table('qx_'.$table)->where($wap)->order("uploadtime desc")->limit($page->firstRow.",".$page->listRows)->select();
		//print_r($model->getlastsql());
		if($_POST)
		{
			$this->assign('choose',$_POST);
		}
		else 
		{
			$tiaojian=$wap;
			//print_r($tiaojian);
			$this->assign('choose',$tiaojian);
		}

		$idcate="";
		switch ($category) 
		{
			case 1:$idcate="pid";break;
			case 2:$idcate="vid";break;
			case 3:$idcate="flaid";break;
			case 4:$idcate="gid";break;
			case 5:$idcate="aid";break;
			case 6:$idcate="iid";break;
			case 7:$idcate="bid";break;
			case 8:$idcate="eid";break;
			case 9:$idcate="kid";break;
		}
		foreach ($list as $k=>$v)
		{
			$list[$k]['id']=$v[$idcate];
			$listurl = explode(',', $v['url']);
			$list[$k]['url'] = $listurl[0];
			//echo $v['pid'];
		}
		
		$tiaojian=$wap;
		$tiaojian['keyword1']=$keyword;
		$tiaojian['count']=$pgnum;
		$tiaojian['ftime']=$day;
		$tiaojian['stime']=$stime;
		$tiaojian['btime']=$btime_p;
		$tiaojian['etime']=$etime_p;
		$tiaojian['all_key']=$all_key;
		$tiaojian['all_sql']=$all_sql;
		$tiaojian['orderby']=$order;
		$this->assign('choose',$tiaojian);
		//获取最近检索词
		$zkeywords=$_SESSION['zkeyword'];
		$this->assign('zkeywords',$zkeywords);
		$this->assign('list',$list);
		$this->assign('cateid',$category);
		$this->assign('ykey',$keyword);
		$this->assign('page',$page->show());
		$this->display();
	}
	

	
}